// /////////////////////////////////////////////////////////////////////////////
//
// Name:            asGameManager.cpp
// Author:          Michael Bartsch (ZCCdark203)
//
// Desc :           Binds the GameManager class to AngelScript
//
// License:         Copyright (C) 2009 Michael Bartsch and Contributors
//
//                  This program is free software: you can redistribute it
//                  and/or modify it under the terms of the zlib/libpng License.
//                  See main.cpp for conditions of distribution and use.
//
// /////////////////////////////////////////////////////////////////////////////

// Include files
#include "asGameManager.h"

#ifdef __COMPILE_WITH_ANGELSCRIPT__

#include "asGameState.h"
#include "../../core/GameManager.h"


//! Reference factory for the DataStack class.
GameManager* createGameManager()
{
    return GameManager::Instance();
}

//! Wrapper function to allow classes "derived" from the GameState class to be
//! used by the GameManager::changeState.
void changeState(asIScriptObject *object, GameManager *pGameMgr)
{
    ScriptedGameState *state = new ScriptedGameState(object);
    pGameMgr->changeState(state);
    state->drop();
}

//! Wrapper function to allow classes "derived" from the GameState class to be
//! used by the GameManager::pushState.
void pushState(asIScriptObject *object, GameManager *pGameMgr)
{
    ScriptedGameState *state = new ScriptedGameState(object);
    pGameMgr->pushState(state);
    state->drop();
}

//! Binds the GameManager class to AngelScript.
void bindGameManager(asIScriptEngine *engine)
{
    // Forward declarations.
    int r;

    // Bind GameManager class.
    r = engine->RegisterObjectType("GameManager", sizeof(GameManager), asOBJ_REF); assert(r >= 0);

    // Set GameManager behaviour.
    r = engine->RegisterObjectBehaviour("GameManager", asBEHAVE_FACTORY, "GameManager@ f()",
                                        asFUNCTION(createGameManager), asCALL_CDECL); assert(r >= 0);
    r = engine->RegisterObjectBehaviour("GameManager", asBEHAVE_ADDREF, "void f()",
                                        asMETHOD(GameManager, grab), asCALL_THISCALL); assert(r >= 0);
    r = engine->RegisterObjectBehaviour("GameManager", asBEHAVE_RELEASE, "void f()",
                                        asMETHOD(GameManager, drop), asCALL_THISCALL); assert(r >= 0);

    // Bind GameManager class functions.
    r = engine->RegisterGlobalFunction("GameManager& getGameManager()", asFUNCTION(GameManager::Instance),
                                       asCALL_CDECL); assert( r >= 0 );

    r = engine->RegisterObjectMethod("GameManager", "void changeState(GameState @+)",
                                     asMETHODPR(GameManager, changeState, (GameState*), void),
                                     asCALL_THISCALL); assert(r >= 0);
    r = engine->RegisterObjectMethod("GameManager", "void changeState(IGameState @+)",
                                     asFUNCTION(changeState), asCALL_CDECL_OBJLAST); assert(r >= 0);
    r = engine->RegisterObjectMethod("GameManager", "void pushState(GameState @+)",
                                     asMETHODPR(GameManager, pushState, (GameState*), void),
                                     asCALL_THISCALL); assert(r >= 0);
    r = engine->RegisterObjectMethod("GameManager", "void pushState(IGameState @+)",
                                     asFUNCTION(pushState), asCALL_CDECL_OBJLAST); assert(r >= 0);
    r = engine->RegisterObjectMethod("GameManager", "void popState()",
                                     asMETHOD(GameManager, popState), asCALL_THISCALL); assert(r >= 0);

    r = engine->RegisterObjectMethod("GameManager", "AssetManager@ getAssetManager()",
                                     asMETHOD(GameManager, getAssetManager), asCALL_THISCALL); assert(r >= 0);
    r = engine->RegisterObjectMethod("GameManager", "DataStore@ getDataStore()",
                                     asMETHOD(GameManager, getDataStore), asCALL_THISCALL); assert(r >= 0);
    r = engine->RegisterObjectMethod("GameManager", "EntityManager@ getEntityManager()",
                                     asMETHOD(GameManager, getEntityManager), asCALL_THISCALL); assert(r >= 0);
    r = engine->RegisterObjectMethod("GameManager", "EventManager@ getEventManager()",
                                     asMETHOD(GameManager, getEventManager), asCALL_THISCALL); assert(r >= 0);
    r = engine->RegisterObjectMethod("GameManager", "ScriptManager@ getScriptManager()",
                                     asMETHOD(GameManager, getScriptManager), asCALL_THISCALL); assert(r >= 0);
#ifdef __COMPILE_WITH_SFML_AUDIO__
    r = engine->RegisterObjectMethod("GameManager", "SoundManager@ getSoundManager()",
                                     asMETHOD(GameManager, getSoundManager), asCALL_THISCALL); assert(r >= 0);
#endif // __COMPILE_WITH_SFML_AUDIO__

    r = engine->RegisterObjectMethod("GameManager", "bool getIsRunning()",
                                     asMETHOD(GameManager, getIsRunning), asCALL_THISCALL); assert(r >= 0);

    r = engine->RegisterObjectMethod("GameManager", "void setIsRunning(bool)",
                                     asMETHOD(GameManager, setIsRunning), asCALL_THISCALL); assert(r >= 0);
}

#endif // __COMPILE_WITH_ANGELSCRIPT__

// End of File
